﻿using System;
using System.Configuration;
using System.IO;
using System.Web.Configuration;
using System.Web.UI.WebControls;
using Core.AdminMenu;
using Core.Authenticated;
using Core.Library;

namespace CMS
{
    public partial class Default : AuthenticatedPage
    {
        protected int AdminMenuId;

        protected string SecretKey;
        protected string LargeFile;        
        protected string SmallFile;
        protected string Domain;
        protected string AdminUserId;

        protected void Page_Load(object sender, EventArgs e)
        {
            AdminMenuId = ConvertUtility.ToInt32(Request.QueryString["cId"]);

            Page.Title      = ConfigurationManager.AppSettings["SiteName"];      
            ltBread.Text    = BuildBread();            
        }

        private string BuildBread()
        {
            var bread = string.Empty;
            var cId = Utils.GetIdFromRequestString(Utils.ControlId, 0);
            if (cId > 0)
            {
                var m = AdminMenuController.AdminMenu_GetById(cId);
                if (m != null)
                {
                    AdminMenuObject pm = null;
                    try { pm = AdminMenuController.AdminMenu_GetById(m.ParentId); }
                    catch { }

                    bread += "<div class=\"breadCrumbHolder module\">";
                    bread += "<div class=\"breadCrumb module\" style=\"margin-top: 0\">";
                    bread += "<ul>";
                    bread += "<li class=\"firstB\"><a href=\"/Default.aspx\"></a></li>";

                    if (pm != null)
                        bread += string.Format("<li><a href=\"#\">{0}</a></li>", pm.Name);

                    bread += string.Format("<li class=\"lastB\"><a href=\"#\">{0}</a></li>", m.Name);

                    bread += "</ul>";
                    bread += "</div>";
                    bread += "</div>";
                }
            }
            return bread;
        }

        protected override void OnInit(EventArgs e)
        {
            var controlId = ConvertUtility.ToInt32(Request.QueryString["cId"]);
            if (controlId != 0)
            {
                var lst = AdminMenuController.AdminMenu_GetAllByStatusAndUserId(
                                            AdminMenuObject.IsVisibled + AdminMenuObject.IsVisibledWhenRun,
                                            CurrentUser.AdminUserId);

                // check if user control file exist ?
                var menuItem = new AdminMenuObject();
                foreach (var o in lst)
                {
                    if (o.AdminMenuId != controlId) continue;
                    menuItem = o;
                    break;
                }

                if (!string.IsNullOrEmpty(menuItem.CtrlSource))
                {
                    if (!File.Exists(Server.MapPath(menuItem.CtrlSource)))
                    {
                        var ltMessage = new Literal
                        {
                            Text = MessageUtility.ShowMessage(string.Empty,
                                                              string.Format("{0} - Chức năng này đang xây dựng. Vui lòng quay lại sau : )",
                                                                    menuItem.CtrlSource),
                                                                    MessageType.Information)
                        };
                        
                        pHolder.Controls.Add(ltMessage);
                        return;
                    }

                    var curControl = LoadControl(menuItem.CtrlSource);
                    if (curControl != null)
                    {
                        pHolder.Controls.Add(curControl);
                    }
                }
                else
                {
                    pHolder.Controls.Add(LoadControl("/Controls/AccessDenied.ascx"));
                    return;
                }
            }
            else
                Response.Redirect(WebConfigurationManager.AppSettings["DefaultControl"]);
        }
    }
}